import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/widget/tag.dart';

import '../../../constants.dart';
import '../../../models/payees_entity.dart';
import 'logic.dart';

class OrderIncomeCreatePage extends StatelessWidget {
  OrderIncomeCreatePage({Key? key}) : super(key: key);

  final logic = Get.find<OrderIncomeCreateLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
        title: Obx(
            () => logic.isCreate.value ? const Text('收款') : const Text('修改发票')),
      ),
      body: Obx(() =>
          logic.isCreate.value ? _buildCreateView() : _buildInvoiceView()),
    );
  }

  Widget _buildInvoiceView() {
    return Form(
      key: logic.formKey,
      child: ListView(
        children: [
          SizedBox(height: kSpaceSmall),

          // 切换电子普票或者纸质发票
          _buildInvoiceType(),

          // 发票信息
          _buildInvoiceInfo(),

          // 提交按钮
          Padding(
              padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
              child: _buildSubmitButton()),
          SizedBox(height: kSpaceSmall),
        ],
      ),
    );
  }

  Widget _buildCreateView() {
    return Form(
      key: logic.formKey,
      child: ListView(
        children: [
          SizedBox(height: kSpaceMedium),

          // 结算方式
          _buildSettlementMethod(),

          SizedBox(height: kSpaceMedium),

          // 收款信息
          _buildOrderInfo(),

          SizedBox(height: kSpaceMedium),

          // 选择关联案件按钮
          _buildSelectCaseBtn(),

          SizedBox(height: kSpaceMini),

          _buildModuleTip('收款订单应该直接跟案件关联，以便于合同及财务对账，支持多个案件关联'),

          SizedBox(height: kSpaceMedium),

          // 上传账单
          _buildUploadBill(),

          SizedBox(height: kSpaceMini),

          _buildModuleTip('若付款人和合同主体不一致，请上传委托付款说明！'),

          SizedBox(height: kSpaceMini),

          Align(
            alignment: Alignment.centerRight,
            child: Padding(
              padding: EdgeInsets.only(right: kSpaceMedium),
              child: InkWell(
                onTap: () => logic.downloadFile(),
                child: Text(
                  '点击下载委托付款说明模板',
                  style: TextStyle(
                    fontSize: kFontSizeMini,
                    color: AppColor.colorFontBlue,
                    decoration: TextDecoration.underline,
                    decorationColor: AppColor.colorFontBlue,
                  ),
                ),
              ),
            ),
          ),

          SizedBox(height: kSpaceMedium),

          // 订单备注
          _buildOrderRemark(),

          SizedBox(height: kSpaceMedium),

          // 是否开发票开关
          _buildInvoiceSwitch(),

          logic.isInvoice.value
              ? Container(
                  margin: EdgeInsets.symmetric(horizontal: kSpaceMedium),
                  width: double.maxFinite,
                  height: 0.5,
                  color: AppColor.colorLineGrey,
                )
              : Container(),

          // 切换电子普票或者纸质发票
          _buildInvoiceType(),

          // 发票信息
          _buildInvoiceInfo(),

          // 提交按钮
          Padding(
              padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
              child: _buildSubmitButton()),
          SizedBox(height: kSpaceSmall),
        ],
      ),
    );
  }

  // 生成提交按钮小部件
  Widget _buildSubmitButton() {
    return Container(
      height: 50.w,
      margin: EdgeInsets.only(top: kSpaceMedium),
      child: FilledButton(
        onPressed: () => logic.submit(),
        child: const Text('提交'),
      ),
    );
  }

  Widget _buildModuleTitle(String title) {
    return Text(
      title,
      style: TextStyle(
        fontSize: kFontSizeMedium,
        color: AppColor.colorFontBlack,
        fontWeight: AppFont.fontMedium,
      ),
    );
  }

  Widget _buildModuleTip(String tip) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.only(top: 3.w),
            child: SvgPicture.asset(
              'assets/icons/ic_error_grey.svg',
              width: 16.w,
              height: 16.w,
            ),
          ),
          SizedBox(width: 6.w),
          Expanded(
            child: Text(
              tip,
              style: TextStyle(
                fontSize: kFontSizeMini,
                color: AppColor.colorFontGrey,
                fontWeight: AppFont.fontRegular,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildModuleBg(List<Widget> children) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(kRadiusMedium),
        color: Colors.white,
      ),
      padding: EdgeInsets.only(
        left: kSpaceMedium,
        right: kSpaceMedium,
        top: kSpaceMedium,
        bottom: kSpaceTiny,
      ),
      margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: children,
      ),
    );
  }

  // 结算方式下拉选择框
  Widget _buildSettlementMethod() {
    return _buildModuleBg([
      _buildModuleTitle('结算方式'),
      SizedBox(height: kSpaceTiny),
      DropdownButtonFormField2<PayeesItemsOptions>(
        isExpanded: true,
        value: logic.settlementValue.value,
        iconStyleData: const IconStyleData(
            icon: Icon(
          Icons.keyboard_arrow_down_rounded,
          color: AppColor.colorFontBlack,
        )),
        decoration: InputDecoration(
          hintText: '选择结算方式(${logic.payeesName.value})',
          hintStyle: TextStyle(
            fontSize: kFontSizeSmall,
            color: AppColor.colorFontLabelGrey,
            fontWeight: AppFont.fontRegular,
          ),
          border: InputBorder.none,
        ),
        dropdownStyleData: const DropdownStyleData(
          padding: EdgeInsets.all(0),
          elevation: 0,
          decoration: BoxDecoration(
            border: Border.fromBorderSide(
              BorderSide(color: Color(0xFFE3E6EB)),
            ),
            borderRadius: BorderRadius.all(Radius.circular(4)),
            color: Colors.white,
          ),
        ),
        style: TextStyle(
          color: AppColor.colorFontBlack,
          fontSize: kFontSizeSmall,
          fontWeight: AppFont.fontRegular,
        ),
        alignment: Alignment.centerLeft,
        validator: (value) {
          if (value == null) {
            return '请选择结算方式';
          }
          return null;
        },
        onChanged: (value) => logic.settlementValue.value = value,
        items: logic.settlementOptions.asMap().entries.map((e) {
          int index = e.key;
          PayeesItemsOptions item = e.value;
          return DropdownMenuItem<PayeesItemsOptions>(
            value: item,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                index != 0
                    ? Container(
                        height: 1.w,
                        color: const Color(0xFFE3E6EB),
                      )
                    : Container(),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
                  child: Text(
                    '${logic.payeesItemName}-${item.desc}',
                    style: TextStyle(
                      color: AppColor.colorFontBlack,
                      fontSize: kFontSizeSmall,
                      fontWeight: AppFont.fontRegular,
                    ),
                  ),
                ),
                Container(),
              ],
            ),
          );
        }).toList(),
        menuItemStyleData: const MenuItemStyleData(padding: EdgeInsets.all(0)),
      ),
    ]);
  }

  // 收款信息
  Widget _buildOrderInfo() {
    return _buildModuleBg([
      _buildModuleTitle('收款信息'),

      SizedBox(height: 20.w),

      // 收款金额
      Text(
        '收款金额',
        style: TextStyle(
          fontSize: kFontSizeSmall,
          color: AppColor.colorFontBlack,
          fontWeight: AppFont.fontRegular,
        ),
      ),

      SizedBox(height: kSpaceTiny),

      // 收款金额
      TextFormField(
        controller: logic.amountText,
        keyboardType: TextInputType.number,
        decoration: InputDecoration(
          icon: SvgPicture.asset(
            'assets/icons/ic_money.svg',
            width: 40.w,
            height: 40.w,
          ),
          contentPadding: EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
          border: InputBorder.none,
        ),
        validator: (value) {
          if (value == null || value.isEmpty) {
            return '请输入收款金额';
          }
          // 金额正则验证
          if (!RegExp(r'^[0-9]+(.[0-9]{1,2})?$').hasMatch(value)) {
            return '请输入正确的金额';
          }
          return null;
        },
      ),

      Container(
        height: 0.5.w,
        color: AppColor.colorLineGrey,
        width: double.maxFinite,
      ),

      // 付款账户
      // TextFormField(
      //   controller: logic.accountText,
      //   decoration: InputDecoration(
      //     hintText: '输入付款户名全称',
      //     hintStyle: TextStyle(
      //       fontSize: kFontSizeSmall,
      //       color: AppColor.colorFontLabelGrey,
      //       fontWeight: AppFont.fontRegular,
      //     ),
      //     contentPadding: EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
      //     border: InputBorder.none,
      //   ),
      //   validator: (value) {
      //     if (value == null || value.isEmpty) {
      //       return '请输入付款户名全称';
      //     }
      //     return null;
      //   },
      // ),

      Autocomplete<String>(
        optionsBuilder: (TextEditingValue value) {
          logic.getCompanyInfo(value.text, logic.taxpayerCodeText);
          logic.accountText.value = value;
          logic.titleText.value = value;
          return logic.searchOrg(value.text);
        },
        onSelected: (String value) {
          logic.getCompanyInfo(value, logic.taxpayerCodeText);
          logic.accountText.text = value;
          logic.titleText.text = value;
        },
        fieldViewBuilder: (context, controller, focusNode, onFieldSubmitted) {
          return TextFormField(
            style: TextStyle(
              fontSize: kFontSizeSmall,
              color: AppColor.colorFontBlack,
            ),
            controller: logic.accountText,
            focusNode: focusNode,
            decoration: InputDecoration(
              hintText: '输入付款户名全称',
              hintStyle: TextStyle(
                color: AppColor.colorFontLabelGrey,
                fontSize: kFontSizeSmall,
              ),
              border: InputBorder.none,
            ),
            validator: (value) {
              if (value == null || value.isEmpty) {
                return '请输入名称';
              }
              return null;
            },
            onChanged: (value) {
              controller.value = TextEditingValue(text: value);
            },
          );
        },
      ),
    ]);
  }

  // 选择关联案件按钮
  Widget _buildSelectCaseBtn() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      child: Ink(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(kRadiusMedium),
          color: Colors.white,
        ),
        child: InkWell(
          borderRadius: BorderRadius.circular(kRadiusMedium),
          onTap: () {
            if (logic.caseList.isEmpty) {
              EasyLoading.showToast('暂无案件数据');
            } else {
              _showBottomSheet();
            }
          },
          child: Padding(
            padding: EdgeInsets.symmetric(
              horizontal: kSpaceSmall,
              vertical: kSpaceSmall,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '关联案件',
                      style: TextStyle(
                        fontSize: kFontSizeMedium,
                        color: AppColor.colorFontBlack,
                        fontWeight: AppFont.fontRegular,
                      ),
                    ),
                    const Icon(
                      Icons.keyboard_arrow_down,
                      color: AppColor.colorFontBlack,
                    ),
                  ],
                ),
                SizedBox(height: kSpaceMini),
                logic.selectedCases.isNotEmpty
                    ? Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: List.generate(
                          logic.selectedCases.length,
                          (index) => Padding(
                            padding: EdgeInsets.only(top: kSpaceMini),
                            child: Tag(
                              text: logic.selectedCases[index]!.name,
                              textColor: AppColor.colorFontBlue,
                              textSize: kFontSizeMini,
                              padding: EdgeInsets.all(14.w),
                              borderColor: AppColor.colorFontBlue,
                              borderRadius: kRadiusMedium,
                              leftIcon: Icon(
                                Icons.done,
                                size: 15.w,
                                color: AppColor.colorFontBlue,
                              ),
                            ),
                          ),
                        ),
                      )
                    : Text(
                        '请选择案件',
                        style: TextStyle(
                          color: AppColor.colorFontGrey,
                          fontSize: kFontSizeSmall,
                          fontWeight: AppFont.fontRegular,
                        ),
                      ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  // 上传账单
  Widget _buildUploadBill() {
    return _buildModuleBg([
      _buildModuleTitle('上传附件'),
      SizedBox(height: kSpaceMini),
      if (logic.selectedFiles.isNotEmpty)
        Column(
          children: List.generate(
            logic.selectedFiles.length,
            (index) => Container(
              padding: EdgeInsets.only(left: kSpaceMedium),
              margin: EdgeInsets.only(top: kSpaceMini),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(kRadiusMedium),
                color: AppColor.colorTagBlue,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: Text(
                      logic.selectedFiles[index].name,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: kFontSizeSmall,
                        color: AppColor.colorFontBlue,
                        fontWeight: AppFont.fontRegular,
                      ),
                    ),
                  ),
                  IconButton(
                    onPressed: () => logic.deleteFile(index),
                    icon: const Icon(Icons.close, color: Color(0xFFC7C8DB)),
                  ),
                ],
              ),
            ),
          ),
        ),
      FilledButton(
        onPressed: () => {logic.pickFile()},
        style: ElevatedButton.styleFrom(
          foregroundColor: AppColor.colorFontBlue,
          backgroundColor: Colors.transparent,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(kRadiusSmall),
          ),
          padding: EdgeInsets.zero,
        ),
        child: Center(
          child: SvgPicture.asset(
            'assets/buttons/ic_upload.svg',
            width: 111.5.w,
            height: 21.w,
          ),
        ),
      ),
      SizedBox(height: kSpaceMini),
    ]);
  }

  // 订单备注
  Widget _buildOrderRemark() {
    return _buildModuleBg([
      _buildModuleTitle('案由'),
      TextFormField(
        controller: logic.orderRemarkText,
        decoration: InputDecoration(
          hintText: '填写案由，利于事后案由检索',
          hintStyle: TextStyle(
            fontSize: kFontSizeSmall,
            color: AppColor.colorFontLabelGrey,
            fontWeight: AppFont.fontRegular,
          ),
          contentPadding: EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
          border: InputBorder.none,
        ),
        maxLines: 4,
      ),
    ]);
  }

  // 是否开发票开关
  Widget _buildInvoiceSwitch() {
    return _buildModuleBg([
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildModuleTitle('是否开发票'),
          Obx(
            () => Switch(
              value: logic.isInvoice.value,
              onChanged: (value) => logic.isInvoice.value = value,
            ),
          ),
        ],
      ),
      logic.isInvoice.value
          ? Text(
              '专票所有项目都需要填',
              style: TextStyle(
                color: AppColor.colorFontGrey,
                fontSize: kFontSizeSmall,
                fontWeight: AppFont.fontRegular,
              ),
            )
          : Text(
              '不开发票',
              style: TextStyle(
                color: AppColor.colorFontGrey,
                fontSize: kFontSizeSmall,
                fontWeight: AppFont.fontRegular,
              ),
            ),
      SizedBox(height: kSpaceMini)
    ]);
  }

  // 切换电子普票或者纸质发票
  Widget _buildInvoiceType() {
    return Obx(
      () => logic.isInvoice.value
          ? _buildModuleBg([
              _buildModuleTitle('发票类型'),
              SizedBox(height: kSpaceMedium),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Expanded(
                    child: _buildSelectionBtn(
                      '电子普票',
                      'onlineArc',
                      logic.selectedInvoiceType.value == 'onlineArc',
                    ),
                  ),
                  SizedBox(width: kSpaceSmall),
                  Expanded(
                    child: _buildSelectionBtn(
                      '电子专票',
                      'offlineArc',
                      logic.selectedInvoiceType.value == 'offlineArc',
                    ),
                  ),
                ],
              ),
            ])
          : Container(),
    );
  }

  // 发票类型选择按钮
  Widget _buildSelectionBtn(String title, String selectedValue, bool selected) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: selected ? AppColor.colorFontBlue : Colors.transparent,
        ),
        color: selected ? const Color(0xFFF2F3FF) : const Color(0xFFF4F6F9),
        borderRadius: BorderRadius.circular(kRadiusSmall),
      ),
      padding: EdgeInsets.symmetric(
        vertical: kSpaceMini,
        horizontal: kSpaceMedium,
      ),
      child: InkWell(
        onTap: () {
          logic.setSelectedInvoiceType(selectedValue);
        },
        child: Text(
          title,
          textAlign: TextAlign.center,
          style: TextStyle(
            color: selected ? AppColor.colorFontBlue : AppColor.colorFontGrey,
            fontSize: kFontSizeMedium,
          ),
        ),
      ),
    );
  }

  Widget _buildInvoiceItem(String title, Widget child) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: kFontSizeSmall,
            color: AppColor.colorFontBlack,
          ),
        ),
        SizedBox(width: kSpaceMedium),
        Expanded(child: child),
      ],
    );
  }

  // 发票信息
  Widget _buildInvoiceInfo() {
    return Obx(() => logic.isInvoice.value
        ? _buildModuleBg([
            _buildModuleTitle('发票信息'),

            SizedBox(height: kSpaceMini),

            // 电子邮箱输入框
            _buildInvoiceItem(
              '*电子邮箱',
              TextFormField(
                textAlign: TextAlign.right,
                controller: logic.emailText,
                decoration: InputDecoration(
                  hintText: '请输入',
                  hintStyle: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: AppColor.colorFontLabelGrey,
                  ),
                  border: InputBorder.none,
                  contentPadding:
                      EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '请输入电子邮箱';
                  }
                  // 电子邮箱正则验证
                  if (!RegExp(
                          r'^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$')
                      .hasMatch(value)) {
                    return '请输入正确的电子邮箱';
                  }
                  return null;
                },
              ),
            ),

            SizedBox(height: kSpaceTiny),

            // 发票抬头
            _buildInvoiceItem(
              '*发票抬头',
              TextFormField(
                textAlign: TextAlign.right,
                controller: logic.titleText,
                decoration: InputDecoration(
                  hintText: '请输入',
                  hintStyle: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: AppColor.colorFontLabelGrey,
                    fontWeight: AppFont.fontRegular,
                  ),
                  border: InputBorder.none,
                  contentPadding:
                      EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '请输入输入发票抬头';
                  }
                  return null;
                },
              ),
            ),

            SizedBox(height: kSpaceTiny),

            // 纳税人识别号输入框
            _buildInvoiceItem(
              '*纳税人识别号',
              TextFormField(
                textAlign: TextAlign.right,
                controller: logic.taxpayerCodeText,
                decoration: InputDecoration(
                  hintText: '请输入',
                  hintStyle: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: AppColor.colorFontLabelGrey,
                    fontWeight: AppFont.fontRegular,
                  ),
                  border: InputBorder.none,
                  contentPadding:
                      EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '请输入纳税人识别号';
                  }
                  return null;
                },
              ),
            ),

            SizedBox(height: kSpaceTiny),

            // 货物或应税劳务名称
            _buildInvoiceItem(
              '*应税名称',
              DropdownButtonFormField2<String>(
                isExpanded: true,
                value: logic.businessText.text.isNotEmpty
                    ? logic.businessText.text
                    : null,
                items: [
                  '法律服务费',
                  '律师服务费',
                  '法律顾问费',
                  '法律咨询费',
                ]
                    .map((e) => DropdownMenuItem(value: e, child: Text(e)))
                    .toList(),
                onChanged: (value) {
                  logic.businessText.text = value ?? '';
                },
                hint: Container(
                  alignment: Alignment.centerRight,
                  child: Text(
                    '请选择',
                    style: TextStyle(
                      fontSize: kFontSizeSmall,
                      color: AppColor.colorFontLabelGrey,
                      fontWeight: AppFont.fontRegular,
                    ),
                  ),
                ),
                selectedItemBuilder: (BuildContext context) {
                  return [
                    '法律服务费',
                    '律师服务费',
                    '法律顾问费',
                    '法律咨询费',
                  ].map((e) {
                    return Container(
                      alignment: Alignment.centerRight,
                      child: Text(
                        e,
                        textAlign: TextAlign.end,
                        style: TextStyle(
                          fontSize: kFontSizeSmall,
                          color: AppColor.colorFontBlack,
                        ),
                      ),
                    );
                  }).toList();
                },
                decoration: InputDecoration(
                  border: InputBorder.none,
                  contentPadding:
                      EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '请选择应税名称';
                  }
                  return null;
                },
              ),
            ),

            SizedBox(height: kSpaceTiny),

            // 银行账号输入框控制器
            _buildInvoiceItem(
              '银行账号',
              TextFormField(
                textAlign: TextAlign.right,
                controller: logic.bankAccountText,
                decoration: InputDecoration(
                  hintText: '请输入',
                  hintStyle: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: AppColor.colorFontLabelGrey,
                    fontWeight: AppFont.fontRegular,
                  ),
                  border: InputBorder.none,
                  contentPadding:
                      EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
                ),
              ),
            ),

            SizedBox(height: kSpaceTiny),

            // 开户银行输入框控制器
            _buildInvoiceItem(
              '开户银行',
              TextFormField(
                textAlign: TextAlign.right,
                controller: logic.bankNameText,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: '请输入',
                  hintStyle: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: AppColor.colorFontLabelGrey,
                    fontWeight: AppFont.fontRegular,
                  ),
                  border: InputBorder.none,
                  contentPadding:
                      EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
                ),
              ),
            ),

            SizedBox(height: kSpaceTiny),

            // 公司电话输入框控制器
            _buildInvoiceItem(
              '电话',
              TextFormField(
                textAlign: TextAlign.right,
                controller: logic.companyPhoneText,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: '请输入',
                  hintStyle: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: AppColor.colorFontLabelGrey,
                    fontWeight: AppFont.fontRegular,
                  ),
                  border: InputBorder.none,
                  contentPadding:
                      EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
                ),
              ),
            ),

            SizedBox(height: kSpaceTiny),

            // 公司地址输入框控制器
            _buildInvoiceItem(
              '地址',
              TextFormField(
                textAlign: TextAlign.right,
                controller: logic.companyAddressText,
                decoration: InputDecoration(
                  hintText: '请输入',
                  hintStyle: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: AppColor.colorFontLabelGrey,
                    fontWeight: AppFont.fontRegular,
                  ),
                  border: InputBorder.none,
                  contentPadding:
                      EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
                ),
              ),
            ),

            SizedBox(height: kSpaceTiny),

            // 发票备注输入框控制器
            _buildInvoiceItem(
              '备注',
              TextFormField(
                textAlign: TextAlign.right,
                controller: logic.invoiceRemarkText,
                decoration: InputDecoration(
                  hintText: '输入发票备注',
                  hintStyle: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: AppColor.colorFontLabelGrey,
                    fontWeight: AppFont.fontRegular,
                  ),
                  border: InputBorder.none,
                  contentPadding:
                      EdgeInsets.only(top: kSpaceSmall, bottom: kSpaceMini),
                ),
              ),
            ),
          ])
        : Container());
  }

  // 底部关联案件弹窗
  void _showBottomSheet() {
    showModalBottomSheet(
      backgroundColor: Colors.white,
      context: Get.context!,
      isDismissible: false,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(15.w)),
      ),
      builder: (context) {
        return SizedBox(
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.only(
                  left: kSpaceMedium,
                  top: kSpaceTiny,
                  bottom: kSpaceTiny,
                  right: kSpaceTiny,
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '请选择关联案件',
                      style: TextStyle(
                        fontSize: kFontSizeMedium,
                        color: AppColor.colorFontBlack,
                      ),
                    ),
                    IconButton(
                      onPressed: () =>
                          {logic.onCanceledSelectedCase(), Get.back()},
                      icon: SvgPicture.asset(
                        'assets/icons/ic_cancel_black.svg',
                        width: kIconSizeMini,
                      ),
                    ),
                  ],
                ),
              ),
              const Divider(),
              Expanded(
                child: ListView.builder(
                  itemCount: logic.caseList.length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(
                        logic.caseList[index]!.name,
                        style: TextStyle(
                          fontSize: kFontSizeSmall,
                          color: AppColor.colorFontBlack,
                        ),
                      ),
                      subtitle: Text(
                        logic.caseList[index]!.caseDate,
                        style: TextStyle(
                          fontSize: kFontSizeMini,
                          color: AppColor.colorFontGrey,
                        ),
                      ),
                      trailing: Obx(
                        () => logic.selectedCaseIndies.contains(index)
                            ? SvgPicture.asset(
                                'assets/icons/ic_radio_selected.svg',
                                width: 16.w)
                            : SvgPicture.asset(
                                'assets/icons/ic_radio_unselect.svg',
                                width: 16.w),
                      ),
                      onTap: () => logic.onSelectedCase(index),
                    );
                  },
                ),
              ),
              const Divider(),
              Padding(
                padding: EdgeInsets.all(kSpaceMedium),
                child: Row(
                  children: [
                    Expanded(
                      child: FilledButton(
                        onPressed: () => logic.onCreateCase(),
                        child: Container(
                          alignment: Alignment.center,
                          height: 50.w,
                          child: Text(
                            '创建案件',
                            textAlign: TextAlign.center,
                            style: TextStyle(fontSize: kFontSizeMedium),
                          ),
                        ),
                      ),
                    ),
                    SizedBox(width: kSpaceMedium),
                    Expanded(
                      child: Obx(
                        () => FilledButton(
                          onPressed: logic.selectedCaseIndies.isEmpty
                              ? null
                              : () =>
                                  {logic.onConfirmSelectedCase(), Get.back()},
                          child: Container(
                            alignment: Alignment.center,
                            height: 50.w,
                            child: Text(
                              '确认',
                              textAlign: TextAlign.center,
                              style: TextStyle(fontSize: kFontSizeMedium),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
